<?php

namespace App\Repositories;

use App\Exceptions\BasicException;

class UserBanClassifyRepository
{
    /**
     * @Desc:二级开屏列表
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/1 15:17
     */
    public function list($param)
    {
        if ($param['type'] == 0) {
            return $this->firstClassifyList($param);
        } elseif ($param['type'] == 1) {
            return $this->secondClassifyList($param);
        }
    }

    /**
     * @Desc:一级分类列表
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/3 14:15
     */
    public function firstClassifyList($param)
    {
        $list = app('model_user_ban_classify')->where('type', 0)->where('pid', 0)
            ->orderBy('id', 'desc')
            ->paginate($param['page_size'] ?? 10, ['*'], '', $param['page'] ?? 1);
        return $list;
    }

    /**
     * @Desc:二级分类列表
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/3 14:16
     */
    public function secondClassifyList($param)
    {
        $list = app('model_user_ban_classify')
            ->where(function ($query) use ($param) {
                if (isset($param['pid']) && $param['pid'] > 0){
                    $query->where('pid', '=', $param['pid']);
                } else {
                    $query->where('pid', '>', 0);
                }
            })->where('type', 0)->with('classify:id,pid,title')->orderBy('id', 'desc')
            ->paginate($param['page_size'] ?? 10, ['*'], '', $param['page'] ?? 1);

        foreach ($list as &$item) {
            $item->first_title = $item->classify->title;
            unset($item->classify);
        }
        return $list;
    }

    /**
     * @Desc:新增
     * @param $params
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function create($params)
    {
        if ($params['type'] == 1){
            if (!isset($params['pid']) || $params['pid'] == 0){
                throw new BasicException(0, '请填写一级分类!');
            }
        } else {
            if (isset($params['pid'])){
                unset($params['pid']);
            }
        }
        $data = [
            'title' => $params['title'],
            'pid' => $params['pid'] ?? 0,
        ];
        $res = app('model_user_ban_classify')->create($data);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '添加失败');
        }
    }

    /**
     * @Desc:详情
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function info($param)
    {
        $info = app('model_user_ban_classify')
            ->where('id', $param['id'])
            ->first();
        return $info;
    }

    /**
     * @Desc:更新
     * @param $params
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function update($params)
    {
        $id = $params['id'];
        if ($params['type'] == 1){
            if (!isset($params['pid']) || $params['pid'] == 0){
                throw new BasicException(0, '请填写一级分类!');
            }
        } else {
            if (isset($params['pid'])){
                unset($params['pid']);
            }
        }
        $data = [
            'title' => $params['title'],
            'pid' => $params['pid'] ?? 0,
        ];
        $res = app('model_user_ban_classify')->where('id', $id)->update($data);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '编辑失败');
        }
    }

    /**
     * @Desc:删除
     * @param $param
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function delete($param)
    {
        //如果删除的是一级分类,那么应该把二级分类也同步删除
        $ids = app('model_user_ban_classify')->whereIn('id', $param['ids'])->where('type', 0)->where('pid', 0)->pluck('id')->toArray();
        \DB::beginTransaction();
        try {
            if (!empty($ids)){
                app('model_user_ban_classify')->whereIn('pid', $ids)->delete();
            }
            app('model_user_ban_classify')->whereIn('id', $param['ids'])->delete();
            \DB::commit();
        }catch (\Exception $exception){
            \DB::rollBack();
            throw new BasicException(0, '删除失败');
        }
    }

    /**
     * @Desc:获取所有的一级分类
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/3 14:23
     */
    public function firstClassify()
    {
        $res = app('model_user_ban_classify')->where('pid', '=', 0)->where('type', 0)->get()->toArray();
        return $res;
    }
}
